#!/bin/sh

if [ -z "$1" ] ; then
    case `echo "$0" | /bin/sed 's/^.*\/\(.*\)/\1/g'` in
	S??*) rc="start" ;;
	K??*) rc="stop" ;;
	*) rc="usage" ;;
    esac
else
    rc="$1"
fi

case "$rc" in
    start)
	echo "starting service postfix"
	
	%OPTWARE_TARGET_PREFIX%/sbin/postfix -c %OPTWARE_TARGET_PREFIX%/etc/postfix start

	# waiting for qmgr
	( i=60
	    while [ -z "$(pidof qmgr)" -a $i -gt 0 ]; do
		i=$(( $i - 1 ))
		sleep 1
	    done
	    echo %OPTWARE_TARGET_PREFIX%/sbin/postfix flush
	) > /dev/null 2>&1 &
	;;
    stop)
	echo "stopping service postfix"
	
	#if [ -f "%OPTWARE_TARGET_PREFIX%/var/spool/postfix/pid/master.pid" ]; then
	#    /bin/kill -TERM `cat %OPTWARE_TARGET_PREFIX%/var/spool/postfix/pid/master.pid` 2>/dev/null
	#fi
	
	if [ -n "`pidof master`" ]; then
	    %OPTWARE_TARGET_PREFIX%/sbin/postfix -c %OPTWARE_TARGET_PREFIX%/etc/postfix stop > /dev/null 2>&1
	fi
	;;
    restart)
	"$0" stop
	sleep 1
	"$0" start
	;;
    *)
	echo "Usage: $0 (start|stop|restart|usage)"
	;;
esac

